/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import abstractmodel.InfoValue;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author fightfxj
 */
public class DetailInfo implements abstractmodel.IInfo {

    String useTimeString;   //时间，直接采用String形式对外表示
    DateFormat useTime;       //收入/支出日期 yyyy-mm-dd 格式，长度为10 的字符串
    String useReason;          //原因 长度不超过20 的字符串
    Double moneyAmount; //数额 绝对值不超过1000000000 的单精度浮点数，精确到小数点后第二位
    Double leftAmount;      //余额
    String notes;                  //备注 长度不超过20 的字符串
    public static final String[] titles = new String[]{"收支日期", "原因", "数额", "余额", "备注"};
    int id;
    int parentID;

    public DetailInfo() {
        useTimeString = new String();
        useTime = DateFormat.getDateInstance();
        useReason = new String();
        moneyAmount = new Double(0);
        leftAmount = new Double(0);
        notes = new String();
    }

    public int getParentID() {
        return parentID;
    }

    public void setParentID(int parentID) {
        this.parentID = parentID;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    public double getLeftAmount() {
        return leftAmount;
    }

    public double getMoneyAmount() {
        return moneyAmount;
    }

    public String getNotes() {
        return notes;
    }

    public String getUseReason() {
        return useReason;
    }

    public String getUseTimeString() {
        return useTimeString;
    }

    public DateFormat getUseTime() {
        return useTime;
    }

    @Override
    public String[] getTitles() {
        return DetailInfo.titles;
    }

    @Override
    public InfoValue getValue(String title) {
        InfoValue iv = new InfoValue();
        iv.setType(this.getClass());
        if (title.equals("收支日期")) {
            iv.setObj(this.useTimeString);
            //iv.setObj(configure.Global.sdf.format(this.useTime.getCalendar().getTime()));
            iv.setType(String.class);
        } else if (title.equals("原因")) {
            iv.setObj(this.useReason);
            iv.setType(String.class);
        } else if (title.equals("数额")) {
            iv.setObj(this.moneyAmount);
            iv.setType(Double.class);
        } else if (title.equals("余额")) {
            iv.setObj(this.leftAmount);
            iv.setType(Double.class);
        } else if (title.equals("备注")) {
            iv.setObj(this.notes);
            iv.setType(String.class);
        } else if (title.equalsIgnoreCase("id")) {
            iv.setObj(new Integer(this.id).toString());
            iv.setType(String.class);
        } else if (title.equalsIgnoreCase("parentID")) {
            iv.setObj(new Integer(this.parentID).toString());
            iv.setType(String.class);
        }
        return iv;
    }

    public boolean checkTime (Object value) {
        DateFormat check = DateFormat.getDateInstance();
        try {
            check.getCalendar().setTime(configure.Global.sdf.parse(new String(value.toString())));   
        }catch (Exception e) {
            String[] options = {"确定"};
                javax.swing.JOptionPane.showOptionDialog(null, "请按照格式填写日期！", 
                    "警告", javax.swing.JOptionPane.DEFAULT_OPTION, 
                    javax.swing.JOptionPane.WARNING_MESSAGE, null, options, options[0]);
            return false;
        }
        return true;
    }
    
    @Override
    public boolean setValue(String title, Object value) {
        if (title.equals("收支日期")) {
            try {
                this.useTime = DateFormat.getDateTimeInstance();
                this.useTime.getCalendar().setTime(configure.Global.sdf.parse(new String(value.toString())));
                System.out.println("fe332trew---2");
                this.useTimeString = value.toString();
            } catch (Exception e) {
                System.out.println("Exception in set time of BankAccountInfo");
            }
        } else if (title.equals("原因")) {
            this.useReason = value.toString();
        } else if (title.equals("数额")) {
            this.moneyAmount = new Double(value.toString()).doubleValue();
        } else if (title.equals("余额")) {
            this.leftAmount = new Double(value.toString()).doubleValue();
        } else if (title.equals("备注")) {
            this.notes = value.toString();
        }
        ModelFactory.data.saveDetailInfo(this);
        return true;
    }

    @Override
    public int getid() {
        return this.id;
    }

    @Override
    public int getType() {
        return configure.Global.DETAIL_INFO_TYPE;
    }
}
